﻿using LinkPad.Helpers;
using LinkPad.Shapes;
using Windows.Devices.Input;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

namespace LinkPad.Handlers
{
    class LinkCreator : CanvasActionHandler
    {
        #region Fields

        private readonly Color _ghostLinkColor = Colors.Purple;
        private Color _normalLinkColor = ArrowedLink.DefaultColor;

        private const double MinLinkLen = 5.0;

        private Point _pressPoint;

        private readonly Line _ghostLink = new Line();

        private uint _penId;

        #endregion

        #region Constructors

        public LinkCreator(ShapeManager shapeManager)
            : base(shapeManager)
        {
            _ghostLink.StrokeThickness = 5.0;
            _ghostLink.Stroke = new SolidColorBrush(_ghostLinkColor);
            _ghostLink.StrokeDashArray = new DoubleCollection {1, 1};
            _ghostLink.StrokeDashOffset = 0.5;
        }
        
        #endregion

        #region Properties

        public override Color MainColor
        {
            get
            {
                return _normalLinkColor;
            }
            set
            {
                _normalLinkColor = value;
            }
        }

        #endregion

        #region Methods

        protected override void HandlePointerPressed(object sender, PointerRoutedEventArgs e)
        {
            // Get information about the pointer location.

            var pt = e.GetCurrentPoint(MyCanvas);

            var pointerDevType = e.Pointer.PointerDeviceType;

            if (pointerDevType == PointerDeviceType.Pen ||
                pointerDevType == PointerDeviceType.Mouse &&
                pt.Properties.IsLeftButtonPressed
                || pointerDevType == PointerDeviceType.Touch)
            {
                _pressPoint = pt.Position;

                _ghostLink.X1 = _pressPoint.X;
                _ghostLink.Y1 = _pressPoint.Y;
                _ghostLink.X2 = _pressPoint.X;
                _ghostLink.Y2 = _pressPoint.Y;

                _penId = pt.PointerId;

                MyCanvas.Children.Add(_ghostLink);

                e.Handled = true;
            }

            base.HandlePointerPressed(sender, e);
        }

        protected override void HandlePointerMoved(object sender, PointerRoutedEventArgs e)
        {
            if (e.Pointer.PointerId == _penId)
            {
                var pt = e.GetCurrentPoint(MyCanvas);

                var currentContactPt = pt.Position;
                var x2 = currentContactPt.X;
                var y2 = currentContactPt.Y;

                // ghost link:
                _ghostLink.X2 = x2;
                _ghostLink.Y2 = y2;
            }

            base.HandlePointerMoved(sender, e);
        }

        protected override void HandlePointerReleased(object sender, PointerRoutedEventArgs e)
        {
            if (e.Pointer.PointerId == _penId)
            {
                var pt = e.GetCurrentPoint(MyCanvas);

                var currentContactPt = pt.Position;
                var x2 = currentContactPt.X;
                var y2 = currentContactPt.Y;

                MyCanvas.Children.Remove(_ghostLink);

                if (_pressPoint.Distance(currentContactPt) >= MinLinkLen)
                {
                    var arrow = new ArrowedLink(ShapeManager)
                    {
                        X1 = _pressPoint.X,
                        Y1 = _pressPoint.Y,
                        X2 = x2,
                        Y2 = y2,
                        ShaftStroke = new SolidColorBrush(_normalLinkColor),
                        ArrowHeadFill = new SolidColorBrush(_normalLinkColor)
                    };

                    ShapeManager.AddShape(arrow);
                }
            }

            _penId = 0;

            e.Handled = true;

            base.HandlePointerReleased(sender, e);
        }


        #endregion
    }
}
